package com.example.android.adapters

import android.content.Context
import android.graphics.BitmapFactory
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import com.example.android.R
import com.example.android.models.MessageView
import com.example.android.utils.MyRecyclerViewAdapter

/**
 * @ClassName OnlineChatMessageViewAdapter
 * @Author Ryan Kamanri
 * @Date 2022/6/13
 * @Description
 **/
class OnlineChatMessageViewAdapter(val context: Context, val messageViewList : ArrayList<MessageView>):
    MyRecyclerViewAdapter<MessageView, RecyclerView.ViewHolder>(messageViewList) {

    inner class SelfMessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
    {
        val message = itemView.findViewById<TextView>(R.id.message)
        val headImage = itemView.findViewById<ImageView>(R.id.HeadImage)

    }

    inner class OtherMessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
    {
        val message = itemView.findViewById<TextView>(R.id.message)
        val headImage = itemView.findViewById<ImageView>(R.id.HeadImage)

    }

    inner class TimeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
    {
        val dateTime = itemView.findViewById<TextView>(R.id.dateTime)
    }

    override fun getItemViewType(position: Int): Int {
        return messageViewList[position].viewType.ordinal
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when(viewType){
            MessageView.ViewType.SELF_TEXT.ordinal ->
                SelfMessageViewHolder(super.CreateView(parent, R.layout.view_right_message))

            MessageView.ViewType.OTHER_TEXT.ordinal ->
                OtherMessageViewHolder(super.CreateView(parent, R.layout.view_left_message))

            else -> TimeViewHolder(super.CreateView(parent, R.layout.view_time_message))
        }
    }

    @RequiresApi(Build.VERSION_CODES.P)
//    @ExperimentalStdlibApi
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val message = messageViewList[position];
        when(holder::class) {
            SelfMessageViewHolder::class -> {
                val mHolder = holder as SelfMessageViewHolder
                mHolder.message.text = message.content as String
//                mHolder.headImage.setImageBitmap(BitmapFactory.decodeByteArray(context.bindHeadImageBytes, 0, context.bindHeadImageBytes.size))
            }

            OtherMessageViewHolder::class -> {
                val mHolder = holder as OtherMessageViewHolder
                mHolder.message.text = message.content as String
//                context.bindFocusUserInfo_HeadImageBytes.forEach {
//                    if(it.userInfo.ID == messageViewList[position].SendUserID)
//                        mHolder.headImage.setImageBitmap(BitmapFactory.decodeByteArray(it.headImageBytes, 0, it.headImageBytes.size))
//                }

            }

            else -> {
                val mHolder = holder as TimeViewHolder
                mHolder.dateTime.text = "${message.time.hours.toString()}:${"%02d".format(message.time.minutes)}"
            }

        }
    }

}